<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jstl/core"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:t="http://myfaces.apache.org/tomahawk"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">
    <f:loadBundle basename="messages" var="msgs"/>
    <body>
        <ui:composition template="/template.xhtml">
            <ui:define name="customs">
                <script type="text/javascript" src="js/autocomplete.js" language="javascript"/>
            </ui:define>
            <ui:define name="title">Verificación de Conteo Paralelo</ui:define>
            <ui:define name="main-content">
                <h:form id="docForm" name="docForm">
                    <center><h:messages id="errorMessages" styleClass="messages" layout="table"/></center>
                    <hr/>
                    <table id="parameters" width="90%" align="center">
                        <tr>
                            <td width="70%">
                                <table id="tabParameters" align="left" width="100%">
                                    <tr align="left">
                                        <td>Municipio: </td>
                                        <td>
                                            <h:selectOneMenu id="townsCombo" value="#{modifyRapidBean.town}" class="SelectOption"
                                                             onchange="this.form.submit();" valueChangeListener="#{modifyRapidBean.processTownChange}">
                                                <t:selectItems id="townOption" value="#{modifyRapidBean.towns}" var="town" itemLabel="#{town.name}" itemValue="#{town}"/>
                                                <f:converter converterId="objconverter"/>
                                            </h:selectOneMenu>
                                            <br/>
                                            <h:outputText id="townName" value=""/>
                                        </td>
                                    </tr>
                                    <tr align="left">
                                        <td>Centro: </td>
                                        <td>
                                            <h:selectOneMenu id="ballotsCombo" value="#{modifyRapidBean.ballotCentre}" class="SelectOption">
                                                <t:selectItems id="ballotOption" value="#{modifyRapidBean.ballots}" var="ballotCentre" itemLabel="#{ballotCentre.description}" itemValue="#{ballotCentre}"/>
                                                <f:converter converterId="objconverter"/>
                                            </h:selectOneMenu>
                                            <br/>
                                            <h:outputText id="ballotDesc" value=""/>
                                        </td>
                                    </tr>
                                    <tr align="left">
                                        <td>Jrv: </td>
                                        <td>
                                            <input type="text" id="autocomplete_jrv" name="autocomplete_jrv"/>
                                        </td>
                                    </tr>
                                    <div id="autocomplete_choices" class="autocomplete"></div> 
                                </table>
                            </td>
                            <td width="30%">
                                <table id="tabCalculations" align="left" width="100%">
                                    <thead>
                                        <tr align="center"><th>Suma de Votos</th></tr>
                                    </thead>
                                    <tr>
                                        <td align="left">Depositados Calculados</td>
                                        <td><h:outputText id="calcDepositedVotes" size="8" value="#{modifyRapidBean.document.calcDepositedVotes}"/></td>
                                    </tr>
                                    <tr>
                                        <td align="left">Válidos Calculados</td>
                                        <td><h:outputText id="calcValidVotes" size="8" value="#{modifyRapidBean.document.validVotes}"/></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                    <table align="center">
                        <td><h:commandButton title="Buscar" action="#{modifyRapidBean.search}" image="images/Search.png"/></td>
                        <td><h:commandButton title="Reiniciar" action="#{modifyRapidBean.reset}" image="images/Redo.png"/></td>
                        <td><h:commandButton title="Guardar" action="#{modifyRapidBean.modify}" image="images/Save.png" onclick="return validations();"/></td>
                    </table>
                    <br/>
                    <table width="70%" align="center">
                        <tr align="center">
                            <th>PLC</th>
                            <th>FSLN</th>
                            <th>PRN</th>
                            <th>ALN</th>
                            <th>AC</th>
                        </tr>
                        <tr>
                            <td align="center">1</td>
                            <td align="center">2</td>
                            <td align="center">7</td>
                            <td align="center">9</td>
                            <td align="center">12</td>
                        </tr>
                        <tr>
                            <td align="center"><img src="images/BANDERA_PLC.jpg" width="75" height="40"/></td>
                            <td align="center"><img src="images/BANDERA_FSLN.jpg" width="75" height="40"/></td>
                            <td align="center"><img src="images/BANDERA_PRN.jpg" width="75" height="40"/></td>
                            <td align="center"><img src="images/BANDERA_ALN.jpg" width="75" height="40"/></td>
                            <td align="center"><img src="images/BANDERA_AC.jpg" width="75" height="40"/></td>
                        </tr>
                        <tr>
                            <td align="center"><h:inputText id="votePlc" size="12" value="#{modifyRapidBean.votePlc}" onkeypress="return validateNumber(event);" onblur="calc();"/></td>
                            <td align="center"><h:inputText id="voteFsln" size="12" value="#{modifyRapidBean.voteFsln}" onkeypress="return validateNumber(event);" onblur="calc();"/></td>
                            <td align="center"><h:inputText id="votePrn" size="12" value="#{modifyRapidBean.votePrn}" onkeypress="return validateNumber(event);" onblur="calc();"/></td>
                            <td align="center"><h:inputText id="voteAln" size="12" value="#{modifyRapidBean.voteAln}" onkeypress="return validateNumber(event);" onblur="calc();"/></td>
                            <td align="center"><h:inputText id="voteAc" size="12" value="#{modifyRapidBean.voteAc}" onkeypress="return validateNumber(event);" onblur="calc();"/></td>
                        </tr>
                    </table>
                    <br/>
                    <table width="80" align="center">
                        <tr width="50%" align="center">
                            <th>Votos Depositados</th>
                            <th>Votos Válidos</th>
                            <th>Votos Nulos</th>
                        </tr>
                        <tr width="50%">
                            <td><h:inputText id="depositedVotes" value="#{modifyRapidBean.document.depositedVotes}" onchange="return calcNulls();"/></td>
                            <td><h:inputText id="validVotes" value="#{modifyRapidBean.document.validVotes}" onchange="return calcNulls();"/></td>
                            <td><h:inputText id="nullVotes" value="#{modifyRapidBean.document.nullVotes}" onchange="return calcNullSingle();"/></td>
                        </tr>
                    </table>
                    <br/>
                    <table width="50" align="center">
                        <tr>
                            <td align="left">Impugnada</td>
                            <td><h:selectBooleanCheckbox title="Impugnada" value="#{modifyRapidBean.document.impugnada}"/></td>
                        </tr>
                    </table>
                    <br/>
                    <h:inputHidden id="operation" value="MODIFICACION"/>
                    <h:inputHidden id="counting" value="#{modifyRapidBean.counting}"/>
                    <h:inputHidden id="jrvHidden" value="#{modifyRapidBean.jrv}">
                        <f:converter converterId="objconverter"/>
                    </h:inputHidden>
                    <c:if test="#{modifyRapidBean.document != null}">
                        <h:inputHidden id="documentId" value="#{modifyRapidBean.document.id}"/>
                        <c:if test="#{modifyRapidBean.document.jrv != null}">
                            <h:inputHidden id="jrvCodeHidden" value="#{modifyRapidBean.document.jrv.code}"/>
                        </c:if>
                    </c:if>
                    <h:inputHidden id="ballotHidden" value="#{modifyRapidBean.ballotCentre}">
                        <f:converter converterId="objconverter"/>
                    </h:inputHidden>
                </h:form>
                <script language="javascript">
                    function validateNumber(evt){
                        if (isNumberKey(evt)){
                            return true;
                        }else{
                            return false;
                        }
                    }
                    
                    function cleanup(){
                        var frm = 'docForm';
                        var documentId = getValue($(frm + ':documentId').value);
                        //alert(documentId);
                        if (documentId == null || documentId == 0){
                            $(frm + ':voteFsln').value = '';
                            $(frm + ':votePlc').value = '';
                            $(frm + ':voteAln').value = '';
                            $(frm + ':votePrn').value = '';
                            $(frm + ':voteAc').value = '';
                            $(frm + ':nullVotes').value = '';
                            $(frm + ':calcDepositedVotes').value = '';
                            $(frm + ':calcValidVotes').value = '';
                            $(frm + ':depositedVotes').value = '';
                            $(frm + ':validVotes').value = '';
                            $(frm + ':nullVotes').value = '';
                            $(frm + ':townsCombo').style.visibility = 'visible';
                            $(frm + ':townName').style.visibility = 'hidden';
                        }else{
                            var jrvCode = getValue($(frm + ':jrvCodeHidden').value);
                            //alert(jrvCode);
                            $('autocomplete_jrv').value = jrvCode;
                            $(frm + ':townsCombo').style.visibility = 'visible';
                            $(frm + ':townName').style.visibility = 'hidden';
                        }
                    }// end of cleanup
                    
                    function calc() {
                        var frm = 'docForm';

                        var voteFsln = getValue($(frm + ':voteFsln').value);
                        var votePlc = getValue($(frm + ':votePlc').value);
                        var voteAln = getValue($(frm + ':voteAln').value);
                        var votePrn = getValue($(frm + ':votePrn').value);
                        var voteAc = getValue($(frm + ':voteAc').value);
                        
                        var nullVotes = getValue($(frm + ':nullVotes').value);
                        if (nullVotes == 0 || nullVotes == null)
                        {
                            nullVotes = 0;
                        }

                        var validVotes = voteFsln + votePlc + voteAln + votePrn + voteAc;
                        var totalVotes = validVotes + nullVotes;
                        
                        $(frm + ':calcDepositedVotes').innerHTML = totalVotes;
                        $(frm + ':calcValidVotes').innerHTML = validVotes;
                        $(frm + ':depositedVotes').value = totalVotes;
                        $(frm + ':validVotes').value = validVotes;
                        $(frm + ':nullVotes').value = nullVotes;
                        
                        if(totalVotes > 400){
                            alert('Suma de votos invalida');
                            return false;
                        }
                        return true;
                    } //end of calc()
                    
                    function calcNulls() {
                        var frm = 'docForm';
                        
                        var voteFsln = getValue($(frm + ':voteFsln').value);
                        var votePlc = getValue($(frm + ':votePlc').value);
                        var voteAln = getValue($(frm + ':voteAln').value);
                        var votePrn = getValue($(frm + ':votePrn').value);
                        var voteAc = getValue($(frm + ':voteAc').value);
                        
                        var depositedVotes = getValue($(frm + ':depositedVotes').value);
                        var validVotes = getValue($(frm + ':validVotes').value);
                        
                        var totalNulls = depositedVotes - validVotes;
                        var calcDepositVotes = voteFsln + votePlc + voteAln + votePrn + voteAc + totalNulls;
                        
                        if (0 > totalNulls){
                            alert('Votos nulos deben ser mayor a cero');
                            return false;
                        }
                        
                        $(frm + ':nullVotes').value = totalNulls;
                        $(frm + ':calcDepositedVotes').innerHTML = calcDepositVotes;
                        return true;
                    }
                    
                    function calcNullSingle(){
                        var frm = 'docForm';
                        
                        var voteFsln = getValue($(frm + ':voteFsln').value);
                        var votePlc = getValue($(frm + ':votePlc').value);
                        var voteAln = getValue($(frm + ':voteAln').value);
                        var votePrn = getValue($(frm + ':votePrn').value);
                        var voteAc = getValue($(frm + ':voteAc').value);
                        
                        var calcValidVotes = voteFsln + votePlc + voteAln + votePrn + voteAc;
                        var unformatedNullVotes = $(frm + ':nullVotes').value;
                        var nullVotes = getValue($(frm + ':nullVotes').value);
                        
                        var vSign = unformatedNullVotes.substr(0,1);
                
                        if(vSign == '-'){
                            nullVotes = nullVotes * -1;
                        }
                        
                        if (0 > nullVotes){
                            alert('Votos nulos deben ser mayor a cero');
                            return false;
                        }
                        
                        var calcDepositedVotes = calcValidVotes + nullVotes;
                        $(frm + ':calcDepositedVotes').innerHTML = calcDepositedVotes;
                        $(frm + ':depositedVotes').value = calcDepositedVotes;
                        $(frm + ':nullVotes').value = nullVotes;
                        return true;
                    }
                    
                    function validations(){
                        var frm = 'docForm';

                        var voteFsln = getValue($(frm + ':voteFsln').value);
                        var votePlc = getValue($(frm + ':votePlc').value);
                        var voteAln = getValue($(frm + ':voteAln').value);
                        var votePrn = getValue($(frm + ':votePrn').value);
                        var voteAc = getValue($(frm + ':voteAc').value);
                        
                        var depositedVotes = getValue($(frm + ':depositedVotes').value);
                        var formValidVotes = getValue($(frm + ':validVotes').value);
                        
                        var unformatedNullVotes = $(frm + ':nullVotes').value;
                        var nullVotes = getValue($(frm + ':nullVotes').value);
                        
                        var vSign = unformatedNullVotes.substr(0,1);
                
                        if(vSign == '-'){
                            nullVotes = nullVotes * -1;
                        }
                        
                        if (nullVotes == 0 || nullVotes == null)
                        {
                            nullVotes = 0;
                        }

                        var validVotes = voteFsln + votePlc + voteAln + votePrn + voteAc;
                        var totalVotes = validVotes + nullVotes;
                        
                        if(totalVotes > 400){
                            alert('Suma de votos invalida');
                            return false;
                        }
                        if (validVotes == 0 || totalVotes == 0){
                            alert('Suma de votos debe ser diferente de cero');
                            return false;
                        }
                        if (0 > validVotes){
                            alert('Votos validos debe ser mayor a cero');
                            return false;
                        }
                        
                        if (0 > totalVotes){
                            alert('Votos validos debe ser mayor a cero');
                            return false;
                        }
                        
                        if (0 > nullVotes){
                            alert('Votos nulos deben ser mayor a cero');
                            return false;
                        }
                        
                        if(totalVotes != depositedVotes){
                            alert('Suma de votos depositados invalida');
                            return false;
                        }
                        
                        if(validVotes != formValidVotes){
                            alert('Suma de votos validos invalida');
                            return false;
                        }
                        return true;
                    }
                    
                    cleanup();
                </script>
            </ui:define>
            <ui:define name="news-content">
                <span class="smalltitle">Registro</span><br/><br />
                
                <span class="smallredtext">Total Registrados</span><br />
                <span class="bodytext">
                    <c:if test="#{currentUser != null}">
                        #{modifyRapidBean.totalInserted} JRVs
                    </c:if>
                </span><br/><br/>
                <span class="smallredtext">Último Insertado</span><br />
                <span class="bodytext">
                    <c:if test="#{currentUser != null}">
                        Jrv: #{modifyRapidBean.lastInsertedDocument.jrv.code}
                        <br/>
                        Votos: #{modifyRapidBean.lastInsertedDocument.calcDepositedVotes}
                    </c:if>
                </span><br/>
            </ui:define>
        </ui:composition>
    </body>
</html>